package com.hzdesk.media.koreabatchmatvector.adapter;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hzdesk.media.koreabatchmatvector.R;
import com.hzdesk.media.koreabatchmatvector.inter.OnMouseListItemClickListener;


/**
 * Created by Administrator on 2018/4/20.
 * 鼠标用户页面的适配器
 */

public class MouseListAdapter extends RecyclerView.Adapter<MouseListAdapter.MyViewHoder> {
    private Context mContext;

    public OnMouseListItemClickListener onMouseListItemClickListener;

    public void setOnMouseListItemClickListener(OnMouseListItemClickListener onMouseListItemClickListener) {
        this.onMouseListItemClickListener = onMouseListItemClickListener;
    }

    public MouseListAdapter(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public MouseListAdapter.MyViewHoder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.mouse_list_item, parent, false);
        MyViewHoder viewHoder = new MyViewHoder(view);
        return viewHoder;
    }

    @Override
    public void onBindViewHolder(MouseListAdapter.MyViewHoder holder, int position) {


        holder.number.setText((position+1) + mContext.getString(R.string.num_muse));
        holder.linearLayout.setOnHoverListener(onHoverListener);
        holder.linearLayout.setOnFocusChangeListener(onFocusChangeListener);
        final int finalPosotion = position;
        holder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onMouseListItemClickListener.OnItemClick(finalPosotion);

            }
        });


    }

    @Override
    public int getItemCount() {
        return 8;
    }


    class MyViewHoder extends RecyclerView.ViewHolder {
        private TextView number;
        private LinearLayout linearLayout;

        public MyViewHoder(View itemView) {
            super(itemView);
            number = itemView.findViewById(R.id.mouse_set_liet_item_number);
            linearLayout = itemView.findViewById(R.id.mouse_set_liet_item_lin);
        }
    }

    private View.OnHoverListener onHoverListener = new View.OnHoverListener() {
        @Override
        public boolean onHover(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_HOVER_ENTER:
                    v.requestFocus();
                    v.requestFocusFromTouch();
                    break;
                case MotionEvent.ACTION_HOVER_EXIT:
                    //v.clearFocus();
                    break;
                case MotionEvent.ACTION_HOVER_MOVE:
                    break;
            }
            return false;
        }
    };

    private View.OnFocusChangeListener onFocusChangeListener = new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            LinearLayout linearLayout = (LinearLayout) v;
            if (hasFocus) {
                linearLayout.setBackgroundResource(R.drawable.selector_view_shape);
                ObjectAnimator scaleX=ObjectAnimator.ofFloat(v,"scaleX",1.0f,1.02f);
                ObjectAnimator scaleY=ObjectAnimator.ofFloat(v,"scaleY",1.0f,1.02f);
                ObjectAnimator translationZ=ObjectAnimator.ofFloat(v,"translationZ",0f,2.0f);
                AnimatorSet animatorSet=new AnimatorSet();
                animatorSet.play(scaleX).with(scaleY).with(translationZ);
                animatorSet.setDuration(200);
                animatorSet.start();
            } else {
                linearLayout.setBackgroundResource(R.drawable.view_shape);
                ObjectAnimator scaleX_01=ObjectAnimator.ofFloat(v,"scaleX",1.02f,1.0f);
                ObjectAnimator scaleY_01=ObjectAnimator.ofFloat(v,"scaleY",1.02f,1.0f);
                ObjectAnimator translationZ_01=ObjectAnimator.ofFloat(v,"translationZ",2.0f,0f);
                AnimatorSet animatorSet_01=new AnimatorSet();
                animatorSet_01.setDuration(200);
                animatorSet_01.play(scaleX_01).with(scaleY_01).with(translationZ_01);
                animatorSet_01.start();
            }

        }
    };

}
